package com.tdam2011.grupo05.services;

import java.util.Date;

import android.content.Context;
import android.util.Log;

import com.tdam2011.grupo05.database.manager.DataManager;
import com.tdam2011.grupo05.entities.account.Account;
import com.tdam2011.grupo05.entities.contact.ContactWeb;
import com.tdam2011.grupo05.entities.message.MessageWeb;
import com.tdam2011.grupo05.entities.thread.ThreadWeb;
import com.tdam2011.grupo05.entities.thread.ThreadWeb.State;

public class ServiceHelper {

	private static final String TAG = "ServiceHelper";
	private static ServiceHelper instance;

	private Context context;

	private final DataManager mDataManager;

	private Listener mListener;

	public interface Listener {
		public void sendMessage(MessageWeb m, ThreadWeb t);

		public void registerUser(String username, String password);

	}

	private ServiceHelper(Context context) {
		this.context = context;
		mDataManager = DataManager.getInstance(context);

	}

	public static ServiceHelper getIntance(Context context) {
		if (instance == null) {
			instance = new ServiceHelper(context);
		}
		instance.setContext(context);
		return instance;
	}

	public void setContext(Context context) {
		this.context = context;

	}

	public Context getContext() {
		return context;
	}

	public void registerAccount(String name, String pass) {
		if (mListener != null) {
			Log.d(TAG, "register new account" + " name=" + name + " pass="
					+ pass);
			mListener.registerUser(name, pass);
		} else {
			Log.e(TAG, "no service subscribed");
		}
	}

	public void sendNewMessage(MessageWeb message, ThreadWeb thread) {
		if (mListener != null) {
			Log.d(TAG, "send new message" + message);
			mListener.sendMessage(message, thread);
		} else {
			Log.e(TAG, "no service subscribed");
		}
	}

	public DataManager getDataManager() {
		return mDataManager;
	}

	public ThreadWeb getSelectThreadById(long idThread) {
		final ThreadWeb t = mDataManager.getThread(idThread);
		if (t.getState() == State.HAVE_UNREAD) {
			t.setState(State.ALL_READED);
			mDataManager.updateThread(t);
		}
		return t;
	}

	public ThreadWeb getThread(long idContact, long idAccount) {
		ThreadWeb thread = mDataManager.getThreadByContact(idContact);
		if (thread == null) {
			thread = new ThreadWeb();
			thread.setDate(new Date());
			thread.setUser(mDataManager.getContactWeb(idContact));
			thread.setName(thread.getUser().getName());
			thread.setState(State.ALL_READED);
			thread.setIdAccount(idAccount);
			thread.setId(mDataManager.saveThread(thread));
		}
		return thread;
	}

	public ContactWeb saveNewContact(ContactWeb contactWeb) {
		long idContact = mDataManager.saveContactWeb(contactWeb);
		contactWeb.setId(idContact);
		return contactWeb;
	}

	public void suscribeListener(Listener l) {
		mListener = l;
	}

	public void unsuscribeListener() {
		mListener = null;
	}

	public long saveNewAccount(Account account) {
		return mDataManager.saveAccount(account);
	}

	public void deleteMessage(long idMessage) {
		MessageWeb m = mDataManager.getMessage(idMessage);
		if (m != null) {
			Log.d(TAG, "deleted!");
			mDataManager.deleteMessage(m);
		}
	}

	public void deleteThread(long idThread) {
		ThreadWeb t = mDataManager.getThread(idThread);
		for (MessageWeb m : t.getMessages()) {
			mDataManager.deleteMessage(m);
		}
		mDataManager.deleteThread(t);
	}

}
